package ldh.serialize.type;

import java.io.IOException;

import ldh.serialize.BinarySerialized;
import ldh.serialize.io.ByteBuf;
import ldh.serialize.util.SerializedUtil;

public class BooleanSerialized implements BinarySerialized<Boolean> {

	@Override
	public void write(ByteBuf out, Boolean bb) throws IOException {
		Byte b = null;
		if (bb == null) {
			SerializedUtil.writeByte(out, (byte) 0);
			return;
		}
		SerializedUtil.writeByte(out, (byte) 1);
		b = bb ? (byte)1 : (byte) 0;
		SerializedUtil.writeByte(out, b);
	}

	@Override
	public Boolean read(ByteBuf in) throws IOException {
		byte isNull = SerializedUtil.readByte(in);
		if (isNull < 1) {
			return null;
		}
		byte b = SerializedUtil.readByte(in);
		return b > 0 ? true : false;
	}

	@Override
	public Class<Boolean> getClassType() {
		return Boolean.class;
	}

}
